using System;
using System.Runtime.Serialization;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Pegasus.UnitTests.Runtime.Serialization.Formatters.Xml.XmlFormatter2Tests
{
	/// <summary>
	/// This is a test class for Pegasus.Runtime.Serialization.Formatters.Xml.XmlFormatter2
	/// and will check the serialization of object via the ISerializtion interface
	///</summary>
	[TestClass]
	public class XmlFormatter2ISerializationTests
	{
		// Local Instance Values
		private TestContext m_testContextInstance;

		/// <summary>
		/// Initializes a new instance of the <see cref="T:XmlFormatter2ISerializationTests"/> class.
		/// </summary>
		public XmlFormatter2ISerializationTests()
		{
		}

		/// <summary>
		/// Gets or sets the test context which provides information about and 
		/// functionality for the current test run.
		/// </summary>
		/// <value>The test context.</value>
		public TestContext TestContext
		{
			get
			{
				return m_testContextInstance;
			}
			set
			{
				m_testContextInstance = value;
			}
		}

		/// <summary>
		/// Test the Serialize method (basic object)
		/// </summary>
		[TestMethod]
		public void SerializeISerializableObjectTest1()
		{
			// Build the object to serialize 
			ISerializableObject startObj = new ISerializableObject();
			startObj.Name = XmlFormatter2Helper.StringTestValue;
			startObj.Description = XmlFormatter2Helper.StringTestValue;

			// Send the object through the serializtion and deserializtion process
			ISerializableObject finishedObj = (ISerializableObject) XmlFormatter2Helper.SerializeDeserialize( startObj );

			// Check test results
			Assert.IsNotNull( finishedObj, "Deserialize return null object" );
			Assert.AreEqual( finishedObj.Name, XmlFormatter2Helper.StringTestValue, "Name value is not right" );
			Assert.AreEqual( finishedObj.Description, XmlFormatter2Helper.StringTestValue, "Description value is not right" );
		}

		/// <summary>
		/// This test uses the NonserializationISerializableObject class which impment the ISerializable interface but
		/// does not have the Serializable attribute on the class. The formatter should throw and exception.
		/// </summary>
		[TestMethod]
		[ExpectedException( typeof( SerializationException ), "Should not allow this class to be serialized" )]
		public void SerializeISerializableObjectTest2()
		{
			// Build the object to serialize 
			NonserializationISerializableObject startObj = new NonserializationISerializableObject();

			// Send the object through the serializtion and deserializtion process
			NonserializationISerializableObject finishedObj = 
				(NonserializationISerializableObject) XmlFormatter2Helper.SerializeDeserialize( startObj );
		}
	}
}
